package com.weddingee.dao.home;
import com.weddingee.dao.base.BaseDao;
import com.weddingee.exception.DBException;
import com.weddingee.exception.DaoException;
import com.weddingee.model.Photo;
import com.weddingee.model.Photo;
import com.weddingee.model.Photo;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;

public class PhotoDao extends BaseDao{
	
	// save
	public Photo save(Photo photo)throws DaoException {
		Connection conn = null;
		PreparedStatement pstm  = null;
			try {
				conn = getConn();
				String sql = "insert into photo values(null,?,?,?)";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				
				//URL
				pstm.setString(1,  photo.getUrl());
				//PHOTOGROUP
				pstm.setLong(2, photo.getGroupId());
				//PHOTONAME
				pstm.setString(3, photo.getName());	
				
				
				int res = pstm.executeUpdate();
				if( res > 0){
					ResultSet rset = pstm.getGeneratedKeys();
					rset.next();
					photo.setId(rset.getLong(1));
					return photo;
				}
				
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error");
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
		
	}

	// list
	public List<Photo> listPhoto(Long groupId) {
		Connection conn = null;
		PreparedStatement pstm  = null;
		
		 List<Photo> photoList = new ArrayList<Photo>();
			try {
				conn = getConn();
				String sql = "SELECT * FROM photo where PHOTOGROUP = ? order by id asc";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				pstm.setLong(1, groupId);
				ResultSet rset = pstm.executeQuery();
				int i=0;
				while(rset.next())
				{
				Photo photo = new Photo();
				photo.setId(rset.getLong(1));
				photo.setUrl(rset.getString(2));
				photo.setGroupId(rset.getLong(3));
				photo.setName(rset.getString(4));
				photoList.add(i,photo);
				i++;
				}
			    return photoList;
				
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error",e);
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
		
		
		
	}
    
	//update
	public Photo update (Photo photo){
		Connection conn = null;
		PreparedStatement pstm  = null;
			try {
				conn = getConn();
				String sql = "update photo set URL=? , PHOTOGROUP=? , PHOTONAME=?  where id= ?";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				//PICURL
				pstm.setString(1, photo.getUrl());
				//PHOTOGROUP
				pstm.setLong(2,photo.getGroupId());
				//PHOTONAME
				pstm.setString(3,  photo.getName());	
				//id
				pstm.setLong(4, photo.getId());
				int res = pstm.executeUpdate();
				return photo;

			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error");
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
	}

	
	// find
	public Photo find(Long photoId) {
		Connection conn = null;
		PreparedStatement pstm  = null;
		Photo photo = new Photo();
			try {
				conn = getConn();
				String sql = "select * from photo where id= ?";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				
				//photoId
				pstm.setLong(1, photoId);
				
				
				ResultSet rset = pstm.executeQuery();
				rset.next();
				photo.setId(photoId);
				photo.setUrl(rset.getString(2));
				photo.setGroupId(rset.getLong(3));
				photo.setName(rset.getString(4));
				return photo;
				
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error",e);
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
		
		
	}

	// delete
	public String delete(Long photoId) {
		Connection conn = null;
		PreparedStatement pstm  = null;
			try {
				conn = getConn();
				String sql = "delete from photo where id= ?";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				
				//photoId
				pstm.setLong(1, photoId);
				int res = pstm.executeUpdate();
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error");
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
	}
}